{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DesignerTextEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, PngImageList, ComCtrls, ExtCtrls, StdCtrls, UIObjects,
  NameValues;

type
  TfrmTextProperties = class(TForm)
    pbHeader: TPaintBox;
    lbCaption: TLabel;
    lbAction: TLabel;
    btCancel: TButton;
    pnItemDetails: TPanel;
    Bevel1: TBevel;
    Bevel3: TBevel;
    btOkay: TButton;
    pgPages: TPageControl;
    tsTranslation: TTabSheet;
    Label2: TLabel;
    Label3: TLabel;
    ecTextName: TComboBox;
    tsNative: TTabSheet;
    mmTranslation: TMemo;
    Label1: TLabel;
    mmNative: TMemo;
    tsMemo: TTabSheet;
    mmMemo: TMemo;
    procedure pbHeaderPaint(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btOkayClick(Sender: TObject);
    procedure UIObjectChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure mmTranslationKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ecTextNameSelect(Sender: TObject);
  private
    { Private declarations }
    FHeaderImages: TPngImageList;
    FOwner: TAHMUIObject;
    FTextName: String;
    FAddingMode: Boolean;
    FContext: String;
    FValueCaption: String;
    procedure LoadImages;
  protected
    { Protected declarations }
    procedure ConfigureForm;
    procedure PopulateForm;
    procedure ValidateForm;
    procedure PopulateObject;
    procedure PopulateCombo(Combo: TComboBox; Value: String; List: TAHMNameValueList);
    procedure PopulateTextControls;
    procedure PopulateMemoControls;
    function ValidateTextControls: Boolean;
    function ValidateMemoControls: Boolean;
    procedure PopulateTextObject;
    procedure PopulateMemoObject;
  public
    { Public declarations }
    property Owner: TAHMUIObject read FOwner write FOwner;
    property TextName: String read FTextName write FTextName;
    property ValueCaption: String read FValueCaption write FValueCaption;
    property AddingMode: Boolean read FAddingMode write FAddingMode;
    class function AddText(var Name: String; Owner: TAHMUIObject): Boolean;
    class function EditText(var Name: String; Owner: TAHMUIObject): Boolean; overload;
    class function EditText(var Name: String; const Caption: String; Owner: TAHMUIObject): Boolean; overload;
  end;


implementation

{$R *.dfm}

uses ApplicationManager, Languages;

var
  FormInstance: TfrmTextProperties;

//-----------------------------------------------------------------------------
//----------------------------- Class Functions -------------------------------
//-----------------------------------------------------------------------------

class function TfrmTextProperties.AddText(var Name: String; Owner: TAHMUIObject): Boolean;
begin
  // Set default result - failed
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmTextProperties.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  FormInstance.Owner := Owner;
  FormInstance.AddingMode := True;

  if FormInstance.ShowModal = mrOK then
  begin
    FormInstance.PopulateObject;
    Name := FormInstance.TextName;
    Result := True;
  end;
end;

class function TfrmTextProperties.EditText(var Name: String; Owner: TAHMUIObject): Boolean;
begin
  // Delegate to method below
  Result := EditText(Name, '', Owner);
end;

class function TfrmTextProperties.EditText(var Name: String; const Caption: String; Owner: TAHMUIObject): Boolean;
begin
  // Set default result - failed
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmTextProperties.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  FormInstance.TextName := Name;
  FormInstance.ValueCaption := Caption;
  FormInstance.Owner := Owner;
  FormInstance.AddingMode := False;

  if FormInstance.ShowModal = mrOK then
  begin
    FormInstance.PopulateObject;
    Name := FormInstance.TextName;
    Result := True;
  end;
end;

//-----------------------------------------------------------------------------
//----------------------------- Form Events -----------------------------------
//-----------------------------------------------------------------------------

procedure TfrmTextProperties.FormCreate(Sender: TObject);
begin
  // Create and populate PNG image list
  FHeaderImages := TPngImageList.Create(Self);
  LoadImages;
end;

procedure TfrmTextProperties.FormShow(Sender: TObject);
begin
  ConfigureForm;
end;

//-----------------------------------------------------------------------------
//----------------------------- Control Events --------------------------------
//-----------------------------------------------------------------------------

procedure TfrmTextProperties.btOkayClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TfrmTextProperties.ecTextNameSelect(Sender: TObject);
begin
  // Update selected language translation
  if FOwner is TAHMLanguage then
    mmTranslation.Text := TAHMLanguage(FOwner).Translate(ecTextName.Text);
end;

procedure TfrmTextProperties.UIObjectChanged(Sender: TObject);
begin
  ValidateForm;
end;

procedure TfrmTextProperties.pbHeaderPaint(Sender: TObject);
begin
  with pbHeader do
  begin
    // Draw background
    Canvas.Brush.Color := clWhite;
    Canvas.FillRect(GetClientRect);
    // Draw appropriate header image
    FHeaderImages.Draw(Canvas, ClientWidth - FHeaderImages.Width, 0, 0);
  end;
end;

procedure TfrmTextProperties.mmTranslationKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  // Allow use of escape key to close form
  if Key = VK_ESCAPE then ModalResult := mrCancel;
end;

//-----------------------------------------------------------------------------
//-------------------------- User Interface Update ----------------------------
//-----------------------------------------------------------------------------

procedure TfrmTextProperties.ConfigureForm;
begin
  // Customise display for selected object type
  if FAddingMode or (FValueCaption = '') then
    if FOwner is TAHMLanguage then FValueCaption := 'Translation'
                              else FValueCaption := 'Text';
  FContext := Lowercase(FValueCaption);

  if FAddingMode then
  begin
    Caption := 'Add ' + FValueCaption;
    lbCaption.Caption := 'Add a new ' + FContext;
    lbAction.Caption := 'You may enter details for the new ' + FContext + ' below';
  end
  else
  begin
    Caption := 'Edit ' + FValueCaption;
    lbCaption.Caption := 'Edit ' + FContext + ' details';
    lbAction.Caption := 'You may modify the ' + FContext + ' below';
  end;

  // Populate form controls
  PopulateForm;
  ValidateForm;

  // Start focus in the page control
  pgPages.SetFocus;
  SelectNext(pgPages, True, True);
end;

procedure TfrmTextProperties.PopulateForm;
var
  i: Integer;
begin
  // Hide tabs for all pages except the active one
  for i := 0 to Pred(pgPages.PageCount) do
    pgPages.Pages[i].TabVisible := False;

  // Display appropriate page of controls
  if FOwner is TAHMLanguage then PopulateTextControls
  else PopulateMemoControls;

  if Assigned(pgPages.ActivePage) then pgPages.ActivePage.TabVisible := True;
end;

procedure TfrmTextProperties.ValidateForm;
var
  Valid: Boolean;
begin
  // Validate appropriate object properties
  if FOwner is TAHMLanguage then Valid := ValidateTextControls
  else Valid := ValidateMemoControls;

  btOkay.Enabled := Valid;
end;

procedure TfrmTextProperties.PopulateObject;
begin
  // Populate appropriate object properties
  if FOwner is TAHMLanguage then PopulateTextObject
  else PopulateMemoObject;
end;

procedure TfrmTextProperties.PopulateCombo(Combo: TComboBox; Value: String; List: TAHMNameValueList);
begin
  with Combo do
  begin
    Items.Clear;
    if Assigned(List) then List.WriteNames(Items);
    if Assigned(List.Parent) then List.Parent.WriteNames(Items);

    if Style = csDropDownList then ItemIndex := Items.IndexOf(Value)
                              else Text := Value;
  end;
end;

procedure TfrmTextProperties.PopulateTextControls;
begin
  with FOwner as TAHMLanguage do
  begin
    if AddingMode then
    begin
      PopulateCombo(ecTextName, '', Translations);
      mmTranslation.Text := '';
      mmNative.Text := '';
    end
    else
    begin
      PopulateCombo(ecTextName, FTextName, Translations);
      mmTranslation.Text := Translate(FTextName);
      with LanguageManagerInstance do
        mmNative.Text := Languages[NativeLanguageName].Translate(FTextName);
    end;

    tsNative.TabVisible := Name <> LanguageManagerInstance.NativeLanguageName;
  end;

  pgPages.ActivePage := tsTranslation;
end;

procedure TfrmTextProperties.PopulateMemoControls;
begin
  tsMemo.Caption := FValueCaption;
  mmMemo.Text:= FTextName;

  pgPages.ActivePage := tsMemo;
end;

function TfrmTextProperties.ValidateTextControls: Boolean;
begin
  Result := (ecTextName.Text <> '') and (mmTranslation.Lines.Count > 0);
end;

function TfrmTextProperties.ValidateMemoControls: Boolean;
begin
  Result := True;
end;

procedure TfrmTextProperties.PopulateTextObject;
begin
  // Populate text object properties
  FTextName := ecTextName.Text;

  with FOwner as TAHMLanguage do
    UpdateTranslation(FTextName, mmTranslation.Text);
  if tsNative.TabVisible then
    with LanguageManagerInstance do
      Languages[NativeLanguageName].UpdateTranslation(FTextName, mmNative.Text);
end;

procedure TfrmTextProperties.PopulateMemoObject;
begin
  // Populate memo object properties - really just a string
  FTextName := mmMemo.Text;
end;

procedure TfrmTextProperties.LoadImages;
begin
  with FHeaderImages do
  begin
    Clear;
    Width := 150; Height := 57;

    AddResourcePng('HD_IMG_GENERIC'); //0
  end;
end;


end.
